package xin.nick;

import org.opencv.core.CvType;
import org.opencv.core.Mat;

import java.net.URL;

import static org.opencv.core.Core.bitwise_not;
import static org.opencv.core.Core.bitwise_xor;
import static org.opencv.core.CvType.CV_8UC3;
import static org.opencv.highgui.HighGui.imshow;
import static org.opencv.highgui.HighGui.waitKey;
import static org.opencv.imgcodecs.Imgcodecs.imread;
import static org.opencv.imgcodecs.Imgcodecs.imwrite;
import static org.opencv.imgproc.Imgproc.COLOR_RGB2GRAY;
import static org.opencv.imgproc.Imgproc.cvtColor;

/**
 * @author Nick
 * @since 2023/4/18/018
 */
public class Main {

    public static void main(String[] args) {

        // 加载动态库
        URL url = ClassLoader.getSystemResource("lib/opencv_java470.dll");
        System.load(url.getPath());

        // 原本的图片
        String oldFile = "D:\\work-file\\0fbe-pull.png";

        // 输出文件路径
        String outFilePath = "./out.png";

        Mat image = imread(oldFile);

        if (image==null||image.empty()) {
            System.out.println("读取图像失败，图像为空");
            return;
        }

        System.out.println("图像宽x高" + image.cols() + " x " + image.rows());
        imshow("Original Image", image);

        Mat grayImage = new Mat(image.rows(), image.cols(), CvType.CV_8SC1);
        cvtColor(image, grayImage, COLOR_RGB2GRAY);
        imshow("grayImage", grayImage);
        Mat src1 = Mat.zeros(image.rows(),image.cols(), CV_8UC3);
        Mat src2 = Mat.zeros(image.rows(),image.cols(), CV_8UC3);
        bitwise_not(src1, src2);
        Mat dst1 = Mat.zeros(image.rows(),image.cols(), CV_8UC3), dst2, dst3;
//        bitwise_xor(image,src1);
        imshow("src1", src1);
        bitwise_xor(image, grayImage,dst1);
//        bitwise_xor(src2, dst1,dst1);
        imshow("dst1", dst1);




        // 参数1：imageSavePath 需要保存的图片新地址
        // 参数2：需要转换成图片的Mat矩阵
        // 返回值: true 保存成功：false 保存失败
        boolean imwrite = imwrite(outFilePath, dst1);


        waitKey();


    }

}
